<template>
  <div class="container">
    <div class="app-container">
      <el-button class="addPermisson" type="primary" size="small" @click="addPermission(0,1)">添加权限</el-button>
      <el-table
        :data="tableList"
        row-key="id"
        default-expand-all
      >
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" @click="addPermission(row.id,2)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="deletePermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog width="750px" title="新增权限点" :visible.sync="showDialog" @close="btnCancel">
      <el-form ref="permissionForm" label-width="120px" :model="permissionForm" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permissionForm.name" style="width:500px" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permissionForm.code" style="width:500px" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="permissionForm.description" style="width:500px" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch v-model="permissionForm.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="10">
              <el-button size="small" type="primary" @click="btnSure">确定</el-button>
              <el-button size="small" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { _addPermission, _deletePermission, _getPermissionDetail, _getPermissionList, _updatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      tableList: [],
      showDialog: false,
      permissionForm: {
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        type: null,
        pid: null
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '权限名称不能为空' }],
        code: [{ required: true, trigger: 'blur', message: '权限标识不能为空' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.tableList = tranListToTreeData(await _getPermissionList(), 0)
      // console.log(this.tableList)
    },
    btnCancel() {
      this.showDialog = false
      this.$refs.permissionForm.resetFields()
    },
    addPermission(pid, type) {
      this.permissionForm.pid = pid
      this.permissionForm.type = type
      this.showDialog = true
    },
    btnSure() {
      this.$refs.permissionForm.validate(async(pass) => {
        if (pass) {
          // 只有修改才会有id,因为新增后才有id
          if (this.permissionForm.id) {
            await _updatePermission(this.permissionForm)
            this.$message.success('修改权限点成功')
          } else {
            await _addPermission(this.permissionForm)
            this.$message.success('新增权限点成功')
          }
          this.getPermissionList()
          this.btnCancel()
        }
      })
    },
    async deletePermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗')
        await _deletePermission(id)
        this.getPermissionList()
        this.$message.success('删除权限点成功')
      } catch (error) {
        console.log(error)
      }
    },
    async editPermission(id) {
      this.showDialog = true
      this.permissionForm = await _getPermissionDetail(id)
    }
  }
}
</script>

<style scoped>
.addPermisson{
  margin:10px;
}
</style>
